<?php

function printCardDetails($cardID)
{
  $card_obj = new Card($cardID);
  $details = $card_obj->getDetails();

  $zIndex = 0;
  
  $carddetails_print = '';
  
  foreach($details->details as $subcard_details)
  {
    $display = $subcard_details->cardside == 1 ? 'block': 'none';
    $carddetails_print .= '<div class="sub_card_details" id="subCard_'.$subcard_details->cardside.'" style="width:223px; display:'.$display.';">';
    
    //image
    $carddetails_print .= '<img src="'.$card_obj->getImageURL($subcard_details->cardside).'">';

    if(sizeof($details->details) > 1)
    {
      $carddetails_print .= '<button onclick="nextCardSide()" style="float:right; border:0px; background:url(./images/flip.png) 0px 0px no-repeat; height:28px; width:35px;"></button>';
    }
    
    //external links
    $multiverseid = $card_obj->getExternalID("multiverseid", $subcard_details->cardside);
    $carddetails_print .= '<div class="external_links" style="clear:both;">
            <h5>External Links</h5>
            <a href="http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid='.$multiverseid.'"><img src="./images/gatherer_favicon.ico"></a>
          </div>';
    
    //name
    $carddetails_print .= '
    <h5>Name:</h5>
    <p>'.$subcard_details->name.'</p>
    ';
   
    //manacost (if available)
    if(!empty($subcard_details->Mana_Cost))
    {
      $carddetails_print .= '<h5>Mana Cost:</h5>';
      $mana_cost = text_to_image_converter::convert_manacost_to_symbol($subcard_details->Mana_Cost);
      $carddetails_print .= '
      <p>'.$mana_cost.'</p>
      ';
      $carddetails_print .= '<h5>Converted Mana Cost:</h5>
      <p>'.$subcard_details->Converted_Mana_Cost.'</p>
      ';
    }
    
    //card type
    $carddetails_print .= '
      <h5>Card Type:</h5>
      <p>'.$subcard_details->Types.'</p>
    ';
    
    //expansion and rarity
    $carddetails_print .= '
      <h5>Expansion - Rarity</h5>
      <p>'.$subcard_details->expansion_name.' - '.$subcard_details->Rarity.'</p>
    ';
    
    //card number
    $carddetails_print .= '
      <h5>Collection number:</h5>
      <p>'.$subcard_details->cardnumber.'</p>
    ';
    
    //card text (if any)
    $cardtext = "";
    foreach($details->card_text as $text)
    {
      if($text[2] === $subcard_details->cardside)
      {
        for($a = 0 ; $a < $text[1] ; $a++)
        {
          $text[0] = text_to_image_converter::convert_cardtext_to_symbol($text[0]);
          $cardtext .= "<p class=\"card_text\">".$text[0]."</p>";
        }
      }
    }
    
    if(!empty($cardtext))
    {
      $carddetails_print .= '<h5>Card Text:</h5>';
      $carddetails_print .= '<div style="background:white; padding:5px; border: 2px solid black;">';
      $carddetails_print .= $cardtext;
      $carddetails_print .= '</div>';
    }
    
    //flavor text (if any)
    if(!empty($subcard_details->Flavor_text))
    {
      $carddetails_print .= '<h5>Flavor text:</h5>';
      $carddetails_print .= '<p class="flavor_text">'.$subcard_details->Flavor_text.'</p>';
    }
    
    //power and tougness (if any)
    if(!empty($subcard_details->power) && !empty($subcard_details->toughness))
    {
      $carddetails_print .= '
      <h5>Power / Toughness</h5>
      <p>'.$subcard_details->power.' / '.$subcard_details->toughness.'</p>
      ';
    }
    
    if(!empty($subcard_details->Loyalty))
    {
      $carddetails_print .= '
      <h5>Loyalty</h5>
      <p>'.$subcard_details->Loyalty.'</p>
      ';
    }
    
    $carddetails_print .= '
    <h5>Artist</h5>
    <p>'.$subcard_details->artist_name.'</p>
    ';
    
    //Cards like this
    $primarytype = substr($subcard_details->Types, 0, strpos($subcard_details->Types, ' '));
    if(empty($primarytype)) { $primarytype = $subcard_details->Types; }
    
    $colors = "";
    $colors_excluded = "";
    
    foreach(array('White', 'Blue', 'Black', 'Red', 'Green') as $color)
    {
      if(strpos($subcard_details->Mana_Cost, $color) !== false)
      {
        $colors .= "&colors[]=".$color;
      }
      else
      {
        $colors_excluded .= "&excluded_colors[]=".$color;
      }
    }
    
    $carddetails_print .= '
    <h5>Cards like this</h5>
    <a href="search.php?manacostlow='.$subcard_details->Converted_Mana_Cost.'&manacosthigh='.$subcard_details->Converted_Mana_Cost.'&type='.$primarytype.''.$colors.''.$colors_excluded.'">Search</a>
    ';
    
    $carddetails_print .= '</div>';
  }
  
  return $carddetails_print;
}

?>